package com.wxy.controller;

import cn.hutool.json.JSONUtil;
import com.wxy.domain.SecurityUser;
import com.wxy.service.SecurityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 47868
 */
@Controller
@RequestMapping("/security")
public class SecurityController {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private SecurityService securityService;

    @GetMapping("/login")
    @Operation(summary = "跳转到login页面方法",description = "跳转到login页面方法")
    @ApiResponse(responseCode = "200",description = "跳转成功",content = @Content)
    public String login() {
        return "login";
    }

    @Operation(summary = "注册方法",description = "注册controller")
    @ApiResponse(responseCode = "200",description = "方法执行成功",content = @Content)
    @PostMapping("/register")
    @ResponseBody
    public String register(HttpServletRequest request) {
        //获取到前端数据
        String registerMessage=request.getParameter("data");
        SecurityUser registerUser= JSONUtil.toBean(registerMessage,SecurityUser.class);
        //默认的权限为consumer
        registerUser.setRole("consumer");
        //将registerUser中的password加密
        String encodePasswordString=passwordEncoder.encode(registerUser.getPassword());
        registerUser.setPassword(encodePasswordString);
        //将注册的数据添加到数据库中
        int num=securityService.InsertUser(registerUser);
        if(num!=0) {
            return "注册成功，请返回登录...";
        }
        return "用户名已存在，请重试...";
    }

    @Operation(summary = "跳转到主页面",description = "跳转到hello.html页面")
    @ApiResponse(responseCode = "200",description = "跳转成功",content = @Content)
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @Operation(summary = "权限页面",description = "boss权限可以访问")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PreAuthorize("hasRole('boss')")
    @GetMapping("/boss")
    public String boss() {
        return "boss";
    }

    @Operation(summary = "权限页面",description = "consumer权限可以访问")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PreAuthorize("hasRole('consumer')")
    @GetMapping("/consumer")
    public String consumer() {
        return "consumer";
    }

    @Operation(summary = "退出登录页面",description = "退出登录后跳转到login页面")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @GetMapping("/logout")
    public String logout() {
        return "login";
    }
}
